<template>
	<view>
		<view class="item" v-for="(res, index) in siteList" :key="res.id" v-if="dataBuffer">
			<u-icon name="close-circle-fill" :size="40" color="#ff5500" :index="res.id" @click="addressDelete(index,res.id)"></u-icon>
			<view class="top">
				<view class="name" @click="backOrderPage(index)">{{ res.username }}</view>
				<view class="phone" @click="backOrderPage(index)">{{ res.userphone }}</view>
				<view class="tag">
					<text class="red" v-if="res.status == 1">默认</text>
				</view>
			</view>
			<view class="bottom">
				{{res.location+res.useraddress}}
				<u-icon name="edit-pen" :size="40" color="#999999" :index="res.id" @click="toEditsitePage(res.id)"></u-icon>
			</view>
		</view>
		<view v-else>
			暂无收货地址信息!
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				siteList: [], //初始化地址数据
				dataBuffer: true
			};
		},
		onShow() {
			this.getAddress()
		},
		methods: {
			// 跳转到编辑页面
			toEditsitePage(id){
				uni.navigateTo({
					url:"./editSite?id="+id
				})
			},
			// 执行删除
			async addressDelete(index,id){
				// console.log(index,id)
				let app =  getApp();
				let header =  app.globalData.header;
				var  option = {
					url:"addressremove",
					data:{
						id,
					},
					header,
					method:"post"
				}
				let res = await this.$http(option);
				// console.log(res)
				if (res.data.code == 403) {
					// 登录过期
					// 过期了
					this.$tips.restore(this, app);
					uni.showModal({
						title: res.data.msg,
						content: "您确定重新登录么？",
						success(result) {
							// console.log(res)
							if (result.confirm) {
								// 同意登录
								uni.reLaunch({
									url: "../user/user"
								})
							} else {
								// 不同意登录
								uni.reLaunch({
									url: "../index/index"
								})
							}
						}
					})
				} else if (res.data.code == 200) {
					// 删除成功
					this.siteList.splice(index,1)
				}
			},
			// 点击地址名称 和手机号，返回确认订单页面，显示对应地址
			backOrderPage(index){
				let address =  JSON.stringify(this.siteList[index]);
				uni.redirectTo({
					url:`../order/order?address=${address}&tag=1`
				})
			},
			async getAddress() {
				let app = getApp()
				let uid = app.globalData.userInfo.uid;
				let header = app.globalData.header;
				var option = {
					url: "addresslist",
					data: {
						uid,
					},
					header
				}
				// 执行查询
				let res = await this.$http(option);
				console.log(res)
				// 过期判断
				if (res.data.code == 403) {
					// 登录过期
					// 过期了
					this.$tips.restore(this, app);
					uni.showModal({
						title: res.data.msg,
						content: "您确定重新登录么？",
						success(result) {
							// console.log(res)
							if (result.confirm) {
								// 同意登录
								uni.reLaunch({
									url: "../user/user"
								})
							} else {
								// 不同意登录
								uni.reLaunch({
									url: "../index/index"
								})
							}
						}
					})
				} else if (res.data.code == 200) {
					// 查询成功
					if (res.data.list == null) {
						// 证明没有一个地址
						this.dataBuffer = false;
					} else {
						this.dataBuffer = true;
						this.siteList = res.data.list
					}
				}
			},

			// 跳转到新增地址页面
			toAddSite() {
				uni.navigateTo({
					url: './addSite'
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import '@/static/css/address.scss';
</style>
